package batch.checkmailup;

import java.sql.*;
import java.util.ArrayList;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.log4j.Logger;

import batch.common.db.ConnectionManager;
import batch.common.email.EmailCheck;
import batch.common.util.Utils;


public class CheckEmailUp{

	
    private String  userNoFrom = "0";
    private String  userNoTo = "0";
	    
	boolean bResult = false;
	
    public CheckEmailUp() {
    ;
    }
    
    private void exec() {
        Logger.getLogger("CheckEmailUp.result").info("Start : "+userNoFrom+" ~ "+userNoTo+" ");	
        bResult = updateMember();		
        Logger.getLogger("CheckEmailUp.result").info("End");
    	
		if(bResult){
	        Logger.getLogger("CheckEmailUp.result").info("SUCCESS BATCH");
		}else{
	        Logger.getLogger("CheckEmailUp.result").info("FAIL BATCH"); //check fail log 
		}
		 
    }

    private boolean updateMember() {
        Connection conn = null;

        int iRE = 0;

        ArrayList params = new ArrayList();

        StringBuffer strQuery = new StringBuffer()
                               .append("SELECT USER_NO, U_EMAIL                                            \n")
                               .append("FROM  G_MB_MEMBER                                                  \n")
                               .append("WHERE USER_NO BETWEEN " + userNoFrom + " AND " + userNoTo  + "     \n")
                               .append("AND U_EMAIL IS NOT NULL AND DELETED IS NULL                        \n")
        					   .append("AND U_EMAIL <> 'wap_temp' AND U_EMAIL <> 'WAP_TEMP' AND REG_COUNTRY IS NOT NULL");//구주,CIS 추가         
        Statement stmt = null;
        ResultSet rs = null;

        
        int i = 0;
        int j = 0;
        String sUserNo = "";
        String sUemail = "";
        int sUflag = 0;

        try{
            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(false);

            QueryRunner qRunner = new QueryRunner();

            stmt = conn.createStatement();
            rs = stmt.executeQuery(strQuery.toString());

            while(rs.next()){
            	i++; 

               sUserNo = String.valueOf(rs.getLong(1));
               sUemail = rs.getString(2);
       		   
               sUflag = new EmailCheck().chkEmail(sUemail);

               if (sUflag < 0 || sUflag == 99){
            	    j++;
            	    strQuery.setLength(0); //init
                	strQuery.append("UPDATE G_MB_MEMBER           \n")
                        	.append("SET U_EMAIL_FLAG = "+sUflag+"\n")
                        	.append("WHERE USER_NO = ?            \n");

                	params.clear();
                	params.add(sUserNo);
                	
                	iRE = qRunner.update(conn, strQuery.toString(), params.toArray());
                	
                	if(iRE != 1){
                		throw new Exception("Member Update Error");
                	}
                	conn.commit(); //commit case by case
                	Logger.getLogger("CheckEmailUp.detail").info("[Total:" + i + "][" + j + "],  UserNo [" + sUserNo+ "], " + "FLAG [" + sUflag+ "] , [" + sUemail+ "] ");
                }
            
            }
 
            bResult = true;            	
            
        }
    
    catch (Exception e) {
		if (conn != null) try { conn.rollback(); } catch (Exception ee) {}    
		Logger.getLogger("CheckEmailUp.detail").info("FAIL here we are [" + i + "],  UserNo [" + sUserNo+ "], [" + sUemail+ "], " + "FLAG [" + sUflag+ "],  EXCEPTION ["+ e.toString() + "]" );
        bResult = false;

    }
    finally {
        if(rs        != null) try { rs.close();        } catch(Exception se) {}
        if(stmt      != null) try { stmt.close();      } catch(Exception se) {}
        if(conn      != null) try { conn.close();      } catch(Exception se) {}  
    }    
    
    return bResult;
    
    }
    

    
    public static void main(String[] args) {
    	CheckEmailUp obj = new CheckEmailUp();
    	obj.userNoFrom   = args[0];
    	obj.userNoTo      = args[1];
    	obj.exec();
    }

}



